package com.kingstar.ceres.data.db.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.kingstar.ceres.data.constant.FileImportConstants;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;

@Getter
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("T_TASK_LOCK")
@ToString
public class TaskLockEntity extends Model<TaskLockEntity> {

    private static final long serialVersionUID=1L;

    /**
     * 主键
     */
    @TableField("ID")
    private String id;

    /**
     * 操作日期
     */
    @TableField("OP_DATE")
    private String opDate;

    /**
     * 任务号;对应T_FILE_MAPPING.FILE_TYPE
     */
    @TableField("TASK_ID")
    private String taskId;

    /**
     * 文件序号;对应T_FILE_MAPPING.ID
     */
    @TableField("FILE_ID")
    private String fileId;

    /**
     * 错误信息
     */
    @TableField("ERROR_MSG")
    private String errorMsg;

    /**
     * 任务状态
     */
    @TableField("TASK_STATUS")
    private String taskStatus;

    /**
     * 执行开始时间
     */
    @TableField("EXEC_START_TIME")
    private LocalDateTime execStartTime;

    /**
     * 执行结束时间
     */
    @TableField("EXEC_END_TIME")
    private LocalDateTime execEndTime;

    /**
     * 最后一次执行类型；manual-手动,auto-自动
     */
    @TableField("LAST_EXEC_TYPE")
    private String lastExecType;

    /**
     * 更新时间
     */
    @TableField("LAST_UPDATED_TIME")
    private LocalDateTime lastUpdatedTime;

    /**
     * 创建时间
     */
    @TableField("CREATE_TIME")
    private LocalDateTime createTime;


    @Override
    public Serializable pkVal() {
        return null;
    }

    protected TaskLockEntity() {
    }

    private TaskLockEntity(String id, String opDate, String taskId,
                           String fileId, String errorMsg, String taskStatus, LocalDateTime execStartTime,
                           LocalDateTime execEndTime, String lastExecType, LocalDateTime lastUpdatedTime,
                           LocalDateTime createTime) {
        this.id = id;
        this.opDate = opDate;
        this.taskId = taskId;
        this.fileId = fileId;
        this.errorMsg = errorMsg;
        this.taskStatus = taskStatus;
        this.execStartTime = execStartTime;
        this.execEndTime = execEndTime;
        this.lastExecType = lastExecType;
        this.lastUpdatedTime = lastUpdatedTime;
        this.createTime = createTime;
    }

    public static TaskLockEntity ofCreate(String opDate,String taskId,String fileId,String lastExecType){
        LocalDateTime now = LocalDateTime.now();
        return new TaskLockEntity(
                null,
                opDate,
                taskId,
                fileId,
                "未执行",
                FileImportConstants.STATUS_UNDO,
                now,
                null,
                lastExecType,
                now,
                now
        );
    }

    public void ofComplete(String successStatus){
        if (Objects.equals(FileImportConstants.STATUS_FAILED,this.taskStatus)){
            return;
        }
        this.modifyTime();
        if (Objects.equals(FileImportConstants.STATUS_ERROR_DOING,this.taskStatus)){
            this.taskStatus=FileImportConstants.STATUS_FAILED;
            return;
        }
        this.taskStatus=successStatus;
        this.errorMsg="执行成功";
    }

    public void editStatus(String taskStatus,String errorMsg){
        this.taskStatus=taskStatus;
        this.errorMsg=errorMsg;
        this.modifyTime();
    }

    private void modifyTime(){
        LocalDateTime now = LocalDateTime.now();
        this.execEndTime=now;
        this.lastUpdatedTime=now;
    }
}